home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 152 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.2 KB  |  66 lines

  1. Path: zetnet.co.uk!demon!polo!john
  2. From: john@polo.demon.co.uk (John Winters)
  3. Newsgroups: comp.std.c
  4. Subject: Re: ... char * * promotion to char const * const * ...
  5. Date: Fri, 19 Jan 1996 19:38:06 GMT
  6. Organization: Wallingford
  7. Message-ID: <DLG17I.8uJ@polo.demon.co.uk>
  8. References: <4dgj8q$qin@unix.sri.com> <KANZE.96Jan17121659@slsvewt.lts.sel.alcatel.de> <DLBzGB.J60@polo.demon.co.uk> <4dns28$3c5@boy.nmd.msu.ru>
  9. X-NNTP-Posting-Host: polo.demon.co.uk
  10.  
  11. In article <4dns28$3c5@boy.nmd.msu.ru>,
  12. Alexander Krotoff <krotoff@such.srcc.msu.su> wrote:
  13. >X-InCommentTo: john@polo.demon.co.uk (John Winters)
  14. >
  15. >john@polo.demon.co.uk (John Winters) wrote:
  16. >> In article <KANZE.96Jan17121659@slsvewt.lts.sel.alcatel.de>,
  17. >> James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de> wrote:
  18. >> [snip]
  19. >> >
  20. >> >Originally, the C standard was going to allow this; in fact, it was
  21. >> >going to allow all casts which added const anywhere in the type.  Then
  22. >> >someone pointed out that the conversion 'char ** -> char *const *' was
  23. >> >unsafe.  As a result, the wording was changed to only allow adding the
  24. >> >const at the top level.
  25. >> 
  26. >> Interesting.  Could you enlighten us (well, me anyway) as to *why* it
  27. >> is unsafe.  I don't find it immediately obvious.
  28. >
  29. >Hello Joshn.
  30.  
  31. Who he?
  32.  
  33. >
  34. >here is an example Joe Buck sent me this Julay.
  35. >i did not understand it too before.
  36. >
  37. [snip]
  38. >
  39. >#include <iostream.h>
  40. >
  41. >int main() {
  42. >    const char ** const_ch_pp;
  43. >    char * ch_p;
  44. >    char ** ch_pp = & ch_p;
  45. >    const char const_ch = 'a';
  46. >    const char * const_ch_p = &const_ch;
  47. >
  48. >    cout << "const_ch_p points to " << *const_ch_p << endl;
  49. >    const_ch_pp = ch_pp;        // illegal, but you want it legal
  50. >    *const_ch_pp = const_ch_p;  // copy const ptr to non-const ptr
  51. >    *ch_p = 'b';                // now we can change the const object
  52. >    cout << "const_ch_p points to " << *const_ch_p << endl;
  53. >}
  54. >
  55.  
  56. Funny, I could have sworn it said comp.std.c on the door.
  57.  
  58. >It's simply not debatable: it is unsafe to assign a char** to a const char**.
  59. >--------------------------------------------------------------------
  60.  
  61. Yes, I understand now.  I go to sleep explaining it to myself. :-)
  62.  
  63. John
  64. -- 
  65. John Winters.  Wallingford, Oxon, England.
  66.